﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace FBProject.BusinessLogic.FBImport
{
    public class ExternalColumnIndexes
    {
        [ExternalColumn("Case")]
        public int TaskNumber { get; set; }

        [ExternalColumn("Title")]
        public int Name { get; set; }

        [ExternalColumn("Estimate (current) - Hours")]
        public int Estimate { get; set; }

        [ExternalColumn("Elapsed Time - Hours")]
        public int ElapsedTime { get; set; }

        [ExternalColumn("Assigned To")]
        public int Resource { get; set; }

        [ExternalColumn("Priority")]
        public int Priority { get; set; }

        [ExternalColumn("Milestone")]
        public int Milestone { get; set; }

        [ExternalColumn("Status")]
        public int Status { get; set; }

        private static Dictionary<string, PropertyInfo> _propertiesWithExternalName = null;
        public static Dictionary<string, PropertyInfo> PropertiesWithExternalName
        {
            get
            {
                if (_propertiesWithExternalName == null)
                    _propertiesWithExternalName = GetPropertiesWithExternalName();
                return _propertiesWithExternalName;
            }
        }

        public ExternalColumnIndexes()
        {
            foreach (var property in ExternalColumnIndexes.PropertiesWithExternalName.Values)
            {
                property.SetValue(this, Int32.MinValue, null);
            }
        }

        private static Dictionary<string, PropertyInfo> GetPropertiesWithExternalName()
        {
            var propertiesByExternalName = new Dictionary<string, PropertyInfo>();

            var properties = typeof(ExternalColumnIndexes).GetProperties();

            foreach (var property in properties)
            {
                var attribute = property.GetCustomAttributes(typeof(ExternalColumnAttribute), false);
                if ((attribute != null) && (attribute.Length > 0))
                {
                    ExternalColumnAttribute externalColumnAttribute = attribute[0] as ExternalColumnAttribute;
                    if ((externalColumnAttribute != null) && (!string.IsNullOrEmpty(externalColumnAttribute.ColumnName)))
                    {
                        propertiesByExternalName.Add(externalColumnAttribute.ColumnName.ToUpperInvariant(), property);
                    }
                }
            }

            return propertiesByExternalName;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public IList<string> GetMissingColumns()
        {
            var missingColumns = new List<string>();

            foreach (var property in ExternalColumnIndexes.PropertiesWithExternalName)
            {
                int propertyValue = (int)property.Value.GetValue(this, null);
                if (propertyValue < 0)
                    missingColumns.Add(property.Key);
            }

            return missingColumns;
        }
    }
}
